package com.sskj.mine.ui.fragment;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.MineFragmentPresenter;
import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;

import javax.inject.Inject;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

@Route(path = RConfig.MINE_MAIN)
public class MineFragment extends BaseFragment<MineFragmentPresenter> {
    @BindView(R2.id.user_head)
    CircleImageView userHead;
    @BindView(R2.id.user_mobile)
    TextView userMobile;
    @BindView(R2.id.mine_login)
    LinearLayout mineLogin;
    @BindView(R2.id.unlogin_show)
    TextView unloginShow;
    @BindView(R2.id.mine_account)
    SuperTextView mineAccount;
    @BindView(R2.id.mine_wallet)
    SuperTextView mineWallet;
    @BindView(R2.id.mine_address)
    SuperTextView mineAddress;
    @BindView(R2.id.mine_zhiwen)
    SuperTextView mineZhiwen;
    @BindView(R2.id.mine_language)
    SuperTextView mineLanguage;
    @BindView(R2.id.mine_about)
    SuperTextView mineAbout;
    @BindView(R2.id.mine_us)
    SuperTextView mineUs;
    @BindView(R2.id.mine_spread)
    SuperTextView mineSpread;
    @BindView(R2.id.smartRefreshLayout)
    CoolRefreshView smartRefreshLayout;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private Boolean isFingerOn;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment_main;
    }

    @Override
    public MineFragmentPresenter getPresenter() {
        return new MineFragmentPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        smartRefreshLayout.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.requestUserInfo();
            }
        });
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                unloginShow.setVisibility(View.GONE);
                userMobile.setText(userData.getMobile());
            } else {
                userData = null;
                unloginShow.setVisibility(View.VISIBLE);
                userMobile.setText(App.INSTANCE.getString(R.string.mine_mineFragment1_j));
            }
        });
        ClickUtil.click(mineLogin, () -> {  // 登录
            if (userData != null) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();
        });
        ClickUtil.click(mineAccount, () -> {  // 托管账户
            if (userData == null) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment2_j));
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_ACCOUNT).navigation();
        });
        ClickUtil.click(mineWallet, () -> {  // 钱包管理
            if (userData == null) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment2_j));
                return;
            }
            ARouter.getInstance().build(RConfig.WALLET_WALLET_MANAGE).navigation();
        });
        ClickUtil.click(mineAddress, () -> {  // 地址簿
            if (userData == null) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment2_j));
                return;
            }
            ARouter.getInstance().build(RConfig.WALLET_ADDRESS_BOOK).navigation();
        });
        isFingerOn = SPUtil.get(SPConfig.IS_FINGER_ON, false);
        mineZhiwen.setRightIcon(isFingerOn ? R.mipmap.mine_icon_on : R.mipmap.mine_icon_off);
        ClickUtil.click(mineZhiwen, () -> {  // 指纹验证
            if (userData == null) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment2_j));
                return;
            }
            isFingerOn = SPUtil.get(SPConfig.IS_FINGER_ON, false);
            if (!isFingerOn) {
                FingerprintIdentify mFingerprintIdentify = new FingerprintIdentify(getActivity());
                if (!mFingerprintIdentify.isHardwareEnable()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment3_j));
                    return;
                }
                if (!mFingerprintIdentify.isRegisteredFingerprint()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment4_j));
                    return;
                }
            }
            mineZhiwen.setRightIcon(!isFingerOn ? R.mipmap.mine_icon_on : R.mipmap.mine_icon_off);
            SPUtil.put(SPConfig.IS_FINGER_ON, !isFingerOn);
        });
        ClickUtil.click(mineLanguage, () -> {  // 语言
            ARouter.getInstance().build(RConfig.MINE_LANGUAGE).navigation();
        });
        ClickUtil.click(mineSpread, () -> {  // 推广
            if (userData == null) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment2_j));
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_SPREAD_MENU).navigation();
        });
        ClickUtil.click(mineAbout, () -> {  // 关于CQTF
            ARouter.getInstance().build(RConfig.MINE_ABOUT).navigation();
        });
        ClickUtil.click(mineUs, () -> {  // 联系我们
            ARouter.getInstance().build(RConfig.MINE_US).navigation();
        });
        ClickUtil.click(mineZhiwen.getRightIconIV(), () -> {  // 指纹开关操作
            if (userData == null) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineFragment2_j));
                return;
            }
            mineZhiwen.getRightIconIV().setImageDrawable(getResources().getDrawable(R.mipmap.mine_icon_off));
        });

    }

    public void updateUserTitle(UserData userData) {
        unloginShow.setVisibility(View.GONE);
        userMobile.setText(userData.getMobile());
        if (smartRefreshLayout != null && smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.setRefreshing(false);
        }
    }
}
